from pyglet.gl import *

from awesum import Drawable, Terrain, Vector3, degToRad, radToDeg

class TrackPart(Terrain):
	"""TrackPart + Drawable interface"""
	def __init__(self, loc, size, coeff=1, eq=0):
		super(TrackPart, self).__init__(Drawable.Traits.GL)
		self.drawRot = 0
		self.drawPos = 0
		self.loc = loc		#cell location in grid (lowest x and z)
		self.size = size	#track size in grid cells
		self.coeff = coeff	#1 or -1 to determine if track is ascending/descending
		self.eq = eq	#flag to represent track equation, if there are options
		self.firstStep = False	#flag for initiating something only on the first step
	
	def __str__(self):
		return "Track Part at: " + str(self.loc)
	
	def draw(self):
		texture = self.game.scene.textures.tex[0]
		
		glEnable(texture.target)
		glBindTexture(texture.target, texture.id)
		glPushMatrix()
		if self.drawPos:
			glTranslatef(self.drawPos.x, self.drawPos.y, self.drawPos.z)
		if self.drawRot: #TODO: remove this and next condition after all parts are converted
			glRotatef(self.drawRot.y, 0., 1., 0.)
			glRotatef(self.drawRot.x, 1., 0., 0.)
			glRotatef(self.drawRot.z, 0., 0., 1.)
		glCallList(self.gl.id)
		glPopMatrix()
		glDisable(texture.target)
		
		if self.game.debugMode:
			left = self.xMin
			right = self.xMax
			bottom = self.yMin
			top = self.yMax
			front = self.zMax
			back = self.zMin
			
			glColor3f(1., 0., 0.)
			glBegin(GL_LINE_STRIP)	#Top square
			glVertex3f(left, top, back)
			glVertex3f(right, top, back)
			glVertex3f(right, top, front)
			glVertex3f(left, top, front)
			glVertex3f(left, top, back)
			glEnd()
			glBegin(GL_LINE_STRIP)	#Bottom square
			glVertex3f(left, bottom, back)
			glVertex3f(right, bottom, back)
			glVertex3f(right, bottom, front)
			glVertex3f(left, bottom, front)
			glVertex3f(left, bottom, back)
			glEnd()
			glBegin(GL_LINES)
			glVertex3f(left, top, back)
			glVertex3f(left, bottom, back)
			glVertex3f(right, top, back)
			glVertex3f(right, bottom, back)
			glVertex3f(right, top, front)
			glVertex3f(right, bottom, front)
			glVertex3f(left, top, front)
			glVertex3f(left, bottom, front)
			glVertex3f(left, top, back)
			glVertex3f(left, bottom, back)
			glEnd()
	
	def step(self):
		self.firstStep = False
	
	#setSize - track size in grid blocks
	def setSize(self, s):
		self.size = s
	
	#yHeight - return absolute Y for track drawing and car pos. x,z are relative
	def yHeight(self, x, z):
		raise NotImplementedError("yHeight not implemented")
	
	#getExits - get a list of vectors for each grid block with an exit
	def getExits(self):
		raise NotImplementedError("getExits not implemented")
	
	@property
	def baseHeight(self):
		return self.loc.y * self.game["BLOCK_SIZE"].y
	
	@property
	def xMin(self):
		return self.loc.x * self.game["BLOCK_SIZE"].x
	@property
	def xMax(self):
		return self.xMin + self.size.x * self.game["BLOCK_SIZE"].x
	@property
	def yMin(self):
		return self.loc.y * self.game["BLOCK_SIZE"].y
	@property
	def yMax(self):
		return self.yMin + self.size.y * self.game["BLOCK_SIZE"].y
	@property
	def zMin(self):
		return self.zMax + self.size.z * self.game["BLOCK_SIZE"].z
	@property
	def zMax(self):
		return self.loc.z * self.game["BLOCK_SIZE"].z
	
	#These properties hold the height of the track at each possible entrance
	@property
	def nLevel(self):
		return self._nLevel
	@property
	def sLevel(self):
		return self._nLevel
	@property
	def eLevel(self):
		return self._nLevel
	@property
	def wLevel(self):
		return self._nLevel